﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;
	using Falcon.Core.SqlServer.Repositories.Readers;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Enums;

	public class ForumTopicReader : IReader<ForumTopic>
	{
		/// <summary>
		/// Populates the specified reader.
		/// </summary>
		/// <param name="reader">The reader.</param>
		/// <param name="entity">The entity.</param>
		public void Populate(SqlDataReader reader, ForumTopic entity)
		{
			entity.Id = reader.GetInt32(0);
			entity.ForumId = reader.GetInt32(1);
			entity.PollId = reader.IsDBNull(2) ? 0 : reader.GetInt32(2);
			entity.Slug = reader.GetString(3);
			entity.Replies = reader.GetInt32(4);
			entity.Views = reader.GetInt32(5);
			entity.Scope = (ForumTopicScope)reader.GetInt32(6);
			entity.Status = (ForumTopicStatus)reader.GetInt32(7);
			entity.Created = reader.GetDateTime(8);
			entity.Updated = reader.GetDateTime(9);

			entity.Forum = new Forum
			{
				Id = reader.GetInt32(10),
				Title = reader.GetString(11),
				Slug = reader.GetString(12),
			};

			if (!reader.IsDBNull(13))
			{
				entity.Post = new ForumPost
				{
					Id = reader.GetInt32(13),
					Title = reader.GetString(14),
					Slug = reader.GetString(15),
					Created = reader.GetDateTime(16),
					Updated = reader.GetDateTime(17),
					User = new User
					{
						Id = reader.GetInt32(18),
						Name = reader.GetString(19),
						Slug = reader.GetString(20),
						Reputation = reader.GetInt32(21),
						Profile = new UserProfile { Title = reader.GetString(22) }
					}
				};

				if (!reader.IsDBNull(23))
				{
					entity.Post.User.Profile.Avatar = new UserAvatar
					{
						FileName = reader.GetString(23),
						FileDiskName = reader.GetString(24),
						FilePath = reader.GetString(25),
						FileType = reader.GetString(26)
					};
				}
			}

			if (!reader.IsDBNull(27))
			{
				entity.LastPost = new ForumPost
				{
					Id = reader.GetInt32(27),
					Title = reader.GetString(28),
					Slug = reader.GetString(29),
					Created = reader.GetDateTime(30),
					Updated = reader.GetDateTime(31),
					User = new User
					{
						Id = reader.GetInt32(32),
						Name = reader.GetString(33),
						Slug = reader.GetString(34),
						Reputation = reader.GetInt32(35),
						Profile = new UserProfile { Title = reader.GetString(36) }
					}
				};

				if (!reader.IsDBNull(37))
				{
					entity.LastPost.User.Profile.Avatar = new UserAvatar
					{
						FileName = reader.GetString(37),
						FileDiskName = reader.GetString(38),
						FilePath = reader.GetString(39),
						FileType = reader.GetString(40)
					};
				}
			}

			if (reader.IsDBNull(41))
			{
				return;
			}

			entity.Log = new Log
			{
				Id = reader.GetInt32(41),
				Action = reader.GetString(42),
				Reason = reader.IsDBNull(43) ? null : reader.GetString(43),
				Created = reader.GetDateTime(44),
				User = new User
				{
					Id = reader.GetInt32(45),
					Name = reader.GetString(46),
					Slug = reader.GetString(47)
				}
			};
		}
	}
}